<template lang="html">
  <div class="ratingselect">
    <div class="rating-type border-1px">
      <span class="block positive" @click="select(2, $event)" :class="{'active': mySelectType===2}">{{desc.all}}<span class="count">{{ratings.length}}</span></span>
      <span class="block positive" @click="select(0, $event)" :class="{'active': mySelectType===0}">{{desc.positive}}<span class="count">{{positives.length}}</span></span>
      <span class="block negative" @click="select(1, $event)" :class="{'active': mySelectType===1}">{{desc.negative}}<span class="count">{{negatives.length}}</span></span>
    </div>
    <div class="switch" :class="{'on': onlyContent}" @click="toggleContent">
      <span class="icon-check_circle"></span>
      <span class="text">只看有内容的评价</span>
    </div>
  </div>
</template>

<script>
import {eventHub} from 'common/js/eventHub';

const POSITIVE = 0;
const NEGATIVE = 1;
const ALL = 2;
export default {
  name: 'ratingselect',
  data(){
    return {
      mySelectType: this.selectType,
      myOnlyContent: this.onlyContent
    }
  },
  props: {
    ratings: {
      type: Array,
      default() {
        return [];
      }
    },
    selectType: {
      type: Number,
      default: ALL
    },
    onlyContent: {
      type: Boolean,
      default: false
    },
    desc: {
      type: Object,
      default() {
        return {
          all: '全部',
          positive: '满意',
          negative: '不满意'
        };
      }
    }
  },
  computed: {
    positives() {
      return this.ratings.filter((rating)=> {
        return rating.rateType === POSITIVE;
      });
    },

    negatives() {
      return this.ratings.filter((rating)=> {
        return rating.rateType === NEGATIVE;
      });
    },
  },
  methods: {
    select(type, event) {
      if (!event._constructed) {
        return;
      }
      this.mySelectType = type;
      eventHub.$emit('ratingtype.select', this.mySelectType);
    },

    toggleContent(event) {
      if (!event._constructed) {
        return;
      }
      this.myOnlyContent = !this.myOnlyContent;
      eventHub.$emit('content.toggle', this.myOnlyContent);
    }
  }
}
</script>

<style lang="scss">
  @import "../../common/scss/default";
  @import "../../common/scss/mixin";

  .ratingselect {
    .rating-type {
      padding: 18px 0;
      margin: 0 18px;
      font-size: 0;
      @include border-1px(rgba($default, .1));
      .block {
        display: inline-block;
        padding: 8px 12px;
        margin-right: 8px;
        line-height: 16px;
        border-radius: 1px;
        font-size: 12px;
        color: $grey;
        .count {
          margin-left: 2px;
          font-size: 8px;
        }
        &.active {
          color: $white;
        }
        &.positive {
          background: rgba($blue, .2);
          &.active {
            background: $blue;
          }
        }
        &.negative {
          background: rgba($grey, .2);
          &.active {
            background: $grey;
          }
        }
      }
    }
    .switch {
      padding: 12px 18px;
      line-height: 24px;
      border-bottom: 1px solid rgba($default, .1);
      color: $grey2;
      font-size: 0;
      &.on {
        .icon-check_circle {
          color: $green;
        }
      }
      .icon-check_circle {
        display: inline-block;
        vertical-align: top;
        margin-right: 4px;
        font-size: 24px;
      }
      .text {
        display: inline-block;
        vertical-align: top;
        font-size: 12px;
      }
    }
  }
</style>
